//
//  RempayCalculator.m
//  Rong360
//
//  Created by tianliwei on 16/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "RempayCalculator.h"

@implementation RempayCalculator

- (void)beginCalculate_old{
    CGFloat p = [self.loan floatValue] * 10000.0;//贷款总额
    CGFloat R = [self.rate floatValue] / 1200.0; //月利率
    CGFloat N = [self.deadline floatValue] * 12.0;//贷款月数
    CGFloat cmonth = ( p * R * pow((1.0 +R),N) ) / ( pow(1.0 + R, N) - 1.0 ) ;
    cmonth = round(cmonth *1000.0)/1000.0;//每月还款 四舍五入保留两位小数
    if (isnan(cmonth)) {
        cmonth = 0.0;
    }
    CGFloat ctotal = cmonth * N;
    ctotal = round(ctotal * 1000.0)/1000.0;//所有还款
    CGFloat crate = ctotal - p;//所有利息
    CGFloat cmonth_rate = crate / N;//月利息
    CGFloat cmonth_loan = cmonth - cmonth_rate;//每月还本金
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 
    NSDateComponents *components = [gregorian components:unitFlags fromDate:self.payfirstDate toDate:self.prepayDate options:0];
    NSInteger months = [components month];
    CGFloat cloaned = months * cmonth_loan;//已还本金
    CGFloat crateed = months * cmonth_rate;//已还利息
    CGFloat ctotaled = months * cmonth;//已还总金额
    
    CGFloat cloan_remain = cmonth_loan * ( N - months);//剩余本金
    CGFloat crate_remain = crate - crateed;//剩余利息
    CGFloat ctotal_remain = ctotal - ctotaled;//剩余还款
    
    self.cloan_remain = [NSString stringWithFormat:@"%0.2lf",cloan_remain];
    self.crate_remain = [NSString stringWithFormat:@"%0.2lf",crate_remain];
    self.ctotal_remain = [NSString stringWithFormat:@"%0.2lf",ctotal_remain];
    
    self.cloaned = [NSString stringWithFormat:@"%0.2lf",cloaned];
    self.crateed = [NSString stringWithFormat:@"%0.2lf",crateed];
    
    self.results = @[self.ctotal_remain,self.cloan_remain,self.crate_remain,self.cloaned,self.crateed];
}

- (void)beginCalculate{
    CGFloat p = [self.loan floatValue] * 10000.0;//贷款总额
    CGFloat R = [self.rate floatValue] / 1200.0; //月利率
    CGFloat N = [self.deadline floatValue] * 12.0;//贷款月数
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:self.payfirstDate toDate:self.prepayDate options:0];
    NSInteger months = [components month];
    CGFloat hkze = p * (1 + R * (N + 1)/2);//还款总额
    CGFloat yhbj = p / N * months;//已还本金
    CGFloat sybj = p - yhbj;//剩余本金
    CGFloat mychbj = p/N;//每月偿还本金
    CGFloat yhlx = months * p * R - months * (months -1)/2 *mychbj * R;//已还利息
    CGFloat sylx = p * R * (N + 1)/2 - yhlx;//剩余利息
    CGFloat syhkze = hkze - yhbj - yhlx;
    self.ctotal_remain = [NSString stringWithFormat:@"%.2lf",syhkze];
    self.cloan_remain = [NSString stringWithFormat:@"%.2lf",sybj];
    self.crate_remain = [NSString stringWithFormat:@"%.2lf",sylx];
    self.cloaned = [NSString stringWithFormat:@"%.2lf",yhbj];
    self.crateed = [NSString stringWithFormat:@"%.2lf",yhlx];
    self.results = @[self.ctotal_remain,self.cloan_remain,self.crate_remain,self.cloaned,self.crateed];
}

@end
